package com.ql.service.socket;

import com.ql.dao.CementMixingDao;
import com.ql.dao.SocketServerDao;
import com.ql.domain.CementMixing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;


@Component
public class Server {
    @Autowired
    CementMixingDao cementMixingDao;
    @Autowired
    HandlerThread handlerThread;
    ArrayList<Socket> s;
    private final int PORT = 65432;
    boolean write;
    String strInputstream = "";

    public Server() {
        s = new ArrayList<Socket>();
    }

    public void start() {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            while (true) {
                // 一旦有堵塞, 则表示服务器与客户端获得了连接
                Socket client = serverSocket.accept();
                // 处理这次连接
                handlerThread.setSocket(client);
            }
        } catch (Exception e) {
            System.out.println("服务器异常: " + e.getMessage());
        }
    }


    public void save(CementMixing cementMixing) {
        try {
            cementMixingDao.insertCementMixing(cementMixing);
            System.out.println("保存中");
        } catch (Exception e) {
            System.out.println("保存失败");
        }
    }

}
